import {
	payOrderAPI
} from '@/common/modules/recharge.js'

export const refillPay = (params) => {
	return new Promise(async (resolve, reject) => {
		const result = await uni.login()
		payOrderAPI({
			payment_code: "wechatpay",
			payment_type: 20,
			code: result.code,
			params
		}).then(async (res) => {
			const params = res.data.respData
			if (res.otherData === 1) {
				// 原生支付
				await uni.requestPayment({
					provider: 'wxpay',
					timeStamp: params.payTimeStamp,
					nonceStr: params.paynonceStr,
					package: params.payPackage,
					signType: params.paySignType,
					paySign: params.paySign,
					success(res) {
						uni.showToast({
							title: "支付成功",
							icon: 'success'
						})
						resolve(true)
					},
					fail(err) {
						uni.showToast({
							title: "支付失败",
							icon: 'error'
						})
						reject("错误:" + JSON.stringify(err))
					}
				})
			} else if (res.otherData === 2) {
				uni.openEmbeddedMiniProgram({
					// 跳转其他小程序
					appId: params.appId, // appid
					path: params.path, // 跳转到小程序的路径
					success(result) {
						// 打开成功
						console.log("跳转支付");
					},
				});
			} else {
				// 其他小程序支付
			}

			console.log(res);
		}).catch(err => {
			reject("错误:" + JSON.stringify(err))
		})

	})
}

// module.exports = {
// 	refillPay
// }